﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace SharePoint.Contrib.Extensions
{
    /// <summary>
    /// Extension methods for the SPListItem class
    /// </summary>
    public static class SPListItemExtensions
    { 
        /// <summary>
        /// Returns the server relative url of the SPListItem, something like '/sites/sharepointcontrib/Lists/MyList/1_.000'
        /// </summary>
        public static string ServerRelativeUrl(this SPListItem instance)
        {
            return instance[SPBuiltInFieldId.FileRef].ToString();
        }
        
        /// <summary>
        /// Returns the url of the displayform, something like '/sites/sharepointcontrib/Lists/MyList/DispForm.aspx?id=1'
        /// </summary>
        public static string DispFormUrl(this SPListItem instance)
        {

            return string.Format(CultureInfo.InvariantCulture, "{0}?id={1}",
                instance.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl,
                instance.ID.ToString());
        }

        /// <summary>
        /// Returns the url of the editform, something like '/sites/sharepointcontrib/Lists/MyList/EditForm.aspx?id=1'
        /// </summary>
        public static string EditFormUrl(this SPListItem instance)
        {

            return string.Format(CultureInfo.InvariantCulture, "{0}?id={1}",
                instance.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].ServerRelativeUrl,
                instance.ID.ToString());
        }

        /// <summary>
        /// Returns the url of the newform, something like '/sites/sharepointcontrib/Lists/MyList/NewForm.aspx?id=1'
        /// </summary>
        public static string NewFormUrl(this SPListItem instance)
        {

            return string.Format(CultureInfo.InvariantCulture, "{0}?id={1}",
                instance.ParentList.Forms[PAGETYPE.PAGE_NEWFORM].ServerRelativeUrl,
                instance.ID.ToString());
        }


        /// <summary>
        /// Converts a field on the SPListItem to the preferred type.
        /// </summary>
        /// <param name="name">The name (internal) of the field</param>
        public static T Field<T>(this SPListItem instance, string name)
        {
            if (instance.Fields.Contains(name))
            {
                T obj = default(T);

                try
                {
                    obj = GenericDataConverter<T>.Convert(instance[name]);
                    return obj;
                }
                catch
                {
                    throw;
                }
            }

            throw new Exception(string.Format(CultureInfo.CurrentCulture,
                "The field with name '{0}' was not found inside the SPListItem.", name));

        }

        /// <summary>
        /// Converts a field on the SPListItem to the preferred type.
        /// </summary>
        /// <param name="id">The id of the field</param>
        public static T Field<T>(this SPListItem instance, Guid id)
        {
            if (instance.Fields.Contains(id))
            {
                T obj = default(T);

                try
                {
                    obj = GenericDataConverter<T>.Convert(instance[id]);
                    return obj;
                }
                catch
                {
                    throw;
                }
            }

            throw new Exception(string.Format(CultureInfo.CurrentCulture,
                "The field with id '{0}' was not found inside the SPListItem.", id));

        }
    }
}
